/* eslint-disable @typescript-eslint/explicit-function-return-type */
// 这个文件实现toast通知功能
import { useState } from 'react'

export type ToastProps = {
  id?: string
  title?: string
  description?: string
  duration?: number
  variant?: 'default' | 'destructive'
}

const TOAST_TIMEOUT = 3000

export const useToast = () => {
  const [toasts, setToasts] = useState<ToastProps[]>([])

  const toast = (props: ToastProps) => {
    const id = crypto.randomUUID()
    const newToast = { ...props, id }
    setToasts((prevToasts) => [...prevToasts, newToast])

    if (props.duration !== Infinity) {
      setTimeout(() => {
        setToasts((prevToasts) => prevToasts.filter((toast) => toast.id !== id))
      }, props.duration || TOAST_TIMEOUT)
    }

    return id
  }

  const dismiss = (toastId?: string) => {
    setToasts((prevToasts) => prevToasts.filter((toast) => toast.id !== toastId))
  }

  return { toast, dismiss, toasts }
}

export { useToast as toast }
